package com.qilu.pe.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qilu.pe.R;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.Coupon;
import com.qilu.pe.dao.http.APIRetrofit;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 运费券fragment
public class CouponTrans1Fragment extends BaseFragment {
    private RecyclerView rcv_list;// 运费券列表

    private CouponTransAdapter adapter;
    private List<Coupon> couponList = new ArrayList<>();

    private String type;

    public CouponTrans1Fragment() {

    }

    public static CouponTrans1Fragment newInstance(String type) {
        CouponTrans1Fragment fragment = new CouponTrans1Fragment();
        Bundle args = new Bundle();
        args.putString("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getString("type");
        adapter = new CouponTransAdapter(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_coupon_trans1, container, false);
        rcv_list = v.findViewById(R.id.rcv_list);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        hideProgress();
        reqCouponList();
        showProgress();

        return v;
    }

    // 请求运费券
    private Disposable reqCouponList() {
        return APIRetrofit.getDefault().reqMyCouponList(Global.HEADER, type)
                .compose(RxUtil.<BaseResult2<List<Coupon>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<Coupon>>>() {
                    @Override
                    public void accept(BaseResult2<List<Coupon>> listResult2) throws Exception {
                        hideProgress();
                        if (listResult2.isSuccess()) {

                        } else {

                        }
                        couponList = listResult2.getData();
                        adapter.setDataList(couponList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class CouponTransAdapter extends ListBaseAdapter<Coupon> {

        public CouponTransAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_coupon_trans;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_money = holder.getView(R.id.tv_money);
            TextView tv_date = holder.getView(R.id.tv_date);
            TextView tv_desc = holder.getView(R.id.tv_desc);
            TextView tv_status = holder.getView(R.id.tv_status);
            TextView tv_to_use = holder.getView(R.id.tv_to_use);

            Coupon c = mDataList.get(position);

            tv_money.setText(c.getPrice() + "元优惠券");
            tv_date.setText(c.getEfficient_time() + "-" + c.getLose_time());
//            tv_desc.setText("运费券");
            // 0 未使用 1 已使用
            if (TextUtils.equals("1", c.getType())) {
                tv_status.setText("已使用");
                tv_to_use.setVisibility(View.GONE);
            } else {
                tv_status.setText("");
                tv_to_use.setVisibility(View.VISIBLE);
            }
        }
    }
}